package com.module.base.widget.picker;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.module.base.utils.DisplayUtils;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.module.base.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bsnl_zhangnx
 * @date 2018/7/19 16:46
 * @desc
 */
public class CommonListPicker<T> extends BaseCommonPicker {
    private ArrayList list2;

    public interface IListSelectListener<T> {
        void onItemSelect(T t);
    }

    public OptionsPickerView<String> optionPickerView;


    public IListSelectListener listSelectListener;



    public CommonListPicker(@NonNull Context context) {
        super(context);
    }


    @Override
    public CommonListPicker init() {
        initPicker(context);

        return this;
    }

    public CommonListPicker addList2(List<List<T>> list2) {
        if (this.list2 == null) {
            this.list2 = new ArrayList();
        }
        this.list2.addAll(list2);
        return this;
    }

    @Override
    public CommonListPicker addLists(List list) {
        if (this.lists == null) {
            this.lists = new ArrayList();
        }
        this.lists.addAll(list);
        return this;
    }

    @Override
    public void show() throws Exception {
        if (optionPickerView == null) {
            initPicker(context);

            if (listSelectListener == null) {
                throw new Exception(" IListSelectListener is null");
            }
        }
        if (optionPickerView != null) {
            optionPickerView.show();
        }
    }


    public CommonListPicker setOnSelectListener(@NonNull IListSelectListener onSelectListener) {
        this.listSelectListener = onSelectListener;
        return this;
    }


    private CommonListPicker initPicker(Context context) {
        optionPickerView = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (listSelectListener != null) {
                    listSelectListener.onItemSelect(lists.get(options1));
                }
            }
        })

                .isDialog(true)
                .setSubmitColor(context.getResources().getColor(R.color.color_picker))
                .setCancelColor(context.getResources().getColor(R.color.color_picker))
                .build();

        Dialog mDialog = optionPickerView.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    DisplayUtils.getScreenWidth(context),
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            optionPickerView.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }
        if (list2 != null) {
            optionPickerView.setPicker(lists, list2);
        } else {
            optionPickerView.setPicker(lists);
        }

        return this;
    }

}
